package clientMain;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.*;
import Pages.*;

/**
 * Runs a client through the application
 * @author Andrew Fox
 *
 */

public class Client extends Thread{
	public boolean verbose=clientGenerator.verbose;
	public double buyProbability;
	public clientInfo userInfo;
	public int sellerRating=0;
	public boolean loggedIn=true;
	public StringBuffer currentURL;
	public StringBuffer lastURL;
	public String currentPageType;
	public TreeMap<Integer,Double> currentBids=new TreeMap<Integer,Double>();	// most recent bid
	public TreeMap<Integer,Double> endedAuctions=new TreeMap<Integer,Double>();	// maxBid
	public TreeMap<Integer,Double> sellingItems=new TreeMap<Integer,Double>();	// maxBid
	public TreeMap<Integer,Double> soldItems=new TreeMap<Integer,Double>();		// maxBid
	public TreeMap<Integer,Double> itemsOfInterest=new TreeMap<Integer,Double>();	// itemRating
	public TreeMap<String,Integer> sellerRatings=new TreeMap<String,Integer>(); // seller Ratings
	public boolean exit=false;
	public double typingSpeed=0;			// characters per millisecond;
	public double typingErrorRate=0;
	public boolean formFiller=false;
	public int loginAttempts=0;
	public double titleWordsFactor=1;
	public double descriptionWordsFactor=1;
	public double sellerRatingFactor=1;
	public double numPicsFactor=1;
	public String message="";
	public Socket sock=new Socket("10.9.77.3",8080);
	public int localPort=sock.getLocalPort();


	Client(double buyProbability, clientInfo userInfo, StringBuffer startURL) throws UnknownHostException, IOException{
		this.userInfo=userInfo;
		this.buyProbability=buyProbability;
		//	this.sock=new Socket(runSettings.hostURL.substring(0,runSettings.hostURL.lastIndexOf(":")),8080);
		//	this.sock=new Socket("10.9.77.3",8080);
		System.out.println("LOCAL PORT "+sock.getLocalPort());
		sock.setKeepAlive(true);
		if (startURL.equals(new StringBuffer("home")))
			this.currentURL=new StringBuffer("home");
		else
			this.currentURL=startURL;
		this.lastURL=startURL;
		//set Typing speed/error rate
		double typeSpeedRand=Math.random()*100;
		for(Double m:clientGenerator.typingSpeedDist.keySet()){
			if (typeSpeedRand<m){
				this.typingSpeed=clientGenerator.typingSpeedDist.get(m)+10*(Math.random()-0.5);
				if(verbose)System.out.println("Typing Speed: "+this.typingSpeed/5+" wpm");
				this.typingSpeed=this.typingSpeed/60000.;
				break;
			}
		}
		for(Double m:clientGenerator.typingErrorRate.keySet()){
			if (this.typingSpeed<m){
				this.typingErrorRate=clientGenerator.typingErrorRate.get(m)+(Math.random()-0.5)*0.001;
				break;
			}
		}
		// form filler (5% chance of being a form filler)
		if (Math.random()>0.9)
			this.formFiller=true;

		this.titleWordsFactor+=(Math.random()-1)*0.1;
		this.descriptionWordsFactor+=(Math.random()-1)*0.1;
		this.sellerRatingFactor+=(Math.random()-1)*0.1;
		this.numPicsFactor+=(Math.random()-1)*0.1;

	}


	public void run(){
		for (int i=0;i<10000;i++){

			if (exit==true){
				clientGenerator.activeClients.remove(userInfo);
				clientGenerator.activeClients.add(userInfo);
				break;
			}
			if(verbose)System.out.println("Page Number: "+i);
			if(verbose)System.out.println(currentURL);
			Page activePage=new Page(currentURL,lastURL,this).toPageType();
			lastURL=currentURL;
			try {
				if (activePage.getPageType().equals("login"))
					currentURL=((LoginPage)activePage).makeDecision();
				else if (activePage.getPageType().equals("userHome"))
					currentURL=((UserHomePage)activePage).makeDecision();
				else if (activePage.getPageType().equals("updateUser"))
					currentURL=((UpdateUserPage)activePage).makeDecision();
				else if (activePage.getPageType().equals("registerUser"))
					currentURL=((RegisterUserPage)activePage).makeDecision();
				else if (activePage.getPageType().equals("item"))
					currentURL=((ItemPage)activePage).makeDecision();
				else if (activePage.getPageType().equals("sellItem"))
					currentURL=((SellItemPage)activePage).makeDecision();
				else if (activePage.getPageType().equals("sellConfirm"))
					currentURL=((SellItemConfirmPage)activePage).makeDecision();
				else if (activePage.getPageType().equals("bidConfirm"))
					currentURL=((BidConfirmPage)activePage).makeDecision();
				else if (activePage.getPageType().equals("browseCategory"))
					currentURL=((BrowsePage)activePage).makeDecision();
				else if (activePage.getPageType().equals("buyItem"))
					currentURL=((BuyItemPage)activePage).makeDecision();
				else if (activePage.getPageType().equals("uploadImages"))
					currentURL=((UploadImagesPage)activePage).makeDecision();
				else if (activePage.getPageType().equals("viewUser"))
					currentURL=((ViewUserPage)activePage).makeDecision();
				else if (activePage.getPageType().equals("confirmBuy"))
					currentURL=((ConfirmBuyPage)activePage).makeDecision();
				else if (activePage.getPageType().equals("logout"))
					currentURL=((LogOutPage)activePage).makeDecision();
				else if (activePage.getPageType().equals("HTML5HomePage"))
					currentURL=((HTML5HomePage)activePage).makeDecision();

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		clientGenerator.activeClients.remove(userInfo);
		try {
			sock.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}



	public Socket getSock(){
		return this.sock;
	}
	public void writeSock(){
		System.out.println(this.sock.getLocalPort());
	}

}
